
<!DOCTYPE html
  PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
   
      <title>16.6.&nbsp;动态导入模块</title>
      <link rel="stylesheet" href="../diveintopython.css" type="text/css">
      <link rev="made" href="mailto:f8dy@diveintopython.org">
      <meta name="generator" content="DocBook XSL Stylesheets V1.52.2">
      <meta name="keywords" content="Python, Dive Into Python, tutorial, object-oriented, programming, documentation, book, free">
      <meta name="description" content="Python from novice to pro">
      <link rel="home" href="../toc/index.html" title="Dive Into Python">
      <link rel="up" href="index.html" title="第&nbsp;16&nbsp;章&nbsp;函数编程">
      <link rel="previous" href="data_centric.html" title="16.5.&nbsp;数据中心思想编程">
      <link rel="next" href="all_together.html" title="16.7.&nbsp;全部放在一起">
   </head>
   <body>
      <table id="Header" width="100%" border="0" cellpadding="0" cellspacing="0" summary="">
         <tr>
            <td id="breadcrumb" colspan="5" align="left" valign="top">导航：<a href="../index.html">起始页</a>&nbsp;&gt;&nbsp;<a href="../toc/index.html">Dive Into Python</a>&nbsp;&gt;&nbsp;<a href="index.html">函数编程</a>&nbsp;&gt;&nbsp;<span class="thispage">动态导入模块</span></td>
            <td id="navigation" align="right" valign="top">&nbsp;&nbsp;&nbsp;<a href="data_centric.html" title="上一页: “数据中心思想编程”">&lt;&lt;</a>&nbsp;&nbsp;&nbsp;<a href="all_together.html" title="下一页: “全部放在一起”">&gt;&gt;</a></td>
         </tr>
         <tr>
            <td colspan="3" id="logocontainer">
               <h1 id="logo"><a href="../index.html" accesskey="1">深入 Python :Dive Into Python 中文版</a></h1>
               <p id="tagline">Python 从新手到专家 [Dip_5.4b_CPyUG_Release]</p>
            </td>
            <td colspan="3" align="right">
               <form id="search" method="GET" action="http://www.google.com/custom">
                  <p><label for="q" accesskey="4">Find:&nbsp;</label><input type="text" id="q" name="q" size="20" maxlength="255" value=""> <input type="submit" value="搜索"><input type="hidden" name="domains" value="woodpecker.org.cn/diveintopython"><input type="hidden" name="sitesearch" value="www.woodpecker.org.cn/diveintopython"></p>
               </form>
            </td>
         </tr>
      </table>
      <!--#include virtual="/inc/ads" -->
      <div class="section" lang="zh_cn">
         <div class="titlepage">
            <div>
               <div>
                  <h2 class="title"><a name="regression.import"></a>16.6.&nbsp;动态导入模块
                  </h2>
               </div>
            </div>
            <div></div>
         </div>
         <div class="abstract">
            <p>好了，大道理谈够了。让我们谈谈动态导入模块吧。</p>
         </div>
         <p>首先，让我们看一看正常的模块导入。<tt class="literal">import <i class="replaceable">module</i></tt> 语法查看搜索路径，根据给定的名字寻找模块并导入它们。你甚至可以这样做：以逗号分割同时导入多个模块，本章代码前几行就是这样做的。
         </p>
         <div class="example"><a name="d0e36476"></a><h3 class="title">例&nbsp;16.13.&nbsp;同时导入多个模块</h3><pre class="programlisting"><span class='pykeyword'>
import</span> sys, os, re, unittest <a name="regression.import.1.1"></a><img src="../images/callouts/1.png" alt="1" border="0" width="12" height="12">
</pre><div class="calloutlist">
               <table border="0" summary="Callout list">
                  <tr>
                     <td width="12" valign="top" align="left"><a href="#regression.import.1.1"><img src="../images/callouts/1.png" alt="1" border="0" width="12" height="12"></a> 
                     </td>
                     <td valign="top" align="left">这里同时导入四个模块：<tt class="filename">sys</tt> (为系统函数和得到命令行参数)、<tt class="filename">os</tt> (为目录列表之类的操作系统函数)、<tt class="filename">re</tt> (为正则表达式)，以及 <tt class="filename">unittest</tt> (为单元测试)。
                     </td>
                  </tr>
               </table>
            </div>
         </div>
         <p>现在让我们用动态导入做同样的事。</p>
         <div class="example"><a name="d0e36501"></a><h3 class="title">例&nbsp;16.14.&nbsp;动态导入模块</h3><pre class="screen">
<tt class="prompt">&gt;&gt;&gt; </tt><span class="userinput">sys = __import__(<span class='pystring'>'sys'</span>)</span>           <a name="regression.import.2.1"></a><img src="../images/callouts/1.png" alt="1" border="0" width="12" height="12">
<tt class="prompt">&gt;&gt;&gt; </tt><span class="userinput">os = __import__(<span class='pystring'>'os'</span>)</span>
<tt class="prompt">&gt;&gt;&gt; </tt><span class="userinput">re = __import__(<span class='pystring'>'re'</span>)</span>
<tt class="prompt">&gt;&gt;&gt; </tt><span class="userinput">unittest = __import__(<span class='pystring'>'unittest'</span>)</span>
<tt class="prompt">&gt;&gt;&gt; </tt><span class="userinput">sys</span>                               <a name="regression.import.2.2"></a><img src="../images/callouts/2.png" alt="2" border="0" width="12" height="12">
<tt class="prompt">&gt;&gt;&gt; </tt><span class="computeroutput">&lt;module 'sys' (built-in)&gt;</span>
<tt class="prompt">&gt;&gt;&gt; </tt><span class="userinput">os</span>
<tt class="prompt">&gt;&gt;&gt; </tt><span class="computeroutput">&lt;module 'os' from '/usr/local/lib/python2.2/os.pyc'&gt;</span>
</pre><div class="calloutlist">
               <table border="0" summary="Callout list">
                  <tr>
                     <td width="12" valign="top" align="left"><a href="#regression.import.2.1"><img src="../images/callouts/1.png" alt="1" border="0" width="12" height="12"></a> 
                     </td>
                     <td valign="top" align="left">内建 <tt class="function">__import__</tt> 函数与 <tt class="literal">import</tt> 语句的既定目标相同，但它是一个真正的函数，并接受一个字符串参数。
                     </td>
                  </tr>
                  <tr>
                     <td width="12" valign="top" align="left"><a href="#regression.import.2.2"><img src="../images/callouts/2.png" alt="2" border="0" width="12" height="12"></a> 
                     </td>
                     <td valign="top" align="left">变量 <tt class="varname">sys</tt> 现在是 <tt class="filename">sys</tt> 模块，和 <tt class="literal">import sys</tt> 的结果完全相同。变量 <tt class="varname">os</tt> 现在是一个 <tt class="filename">os</tt> 模块，等等。
                     </td>
                  </tr>
               </table>
            </div>
         </div>
         <p>因此 <tt class="function">__import__</tt> 导入一个模块，但是是通过一个字符串参数来做到的。依此处讲，你用以导入的仅仅是一个硬编码性的字符串，但它可以是一个变量，或者一个函数调用的结果。并且你指向模块的变量也不必与模块名匹配。你可以导入一系列模块并把它们指派给一个列表。
         </p>
         <div class="example"><a name="d0e36583"></a><h3 class="title">例&nbsp;16.15.&nbsp;动态导入模块列表</h3><pre class="screen">
<tt class="prompt">&gt;&gt;&gt; </tt><span class="userinput">moduleNames = [<span class='pystring'>'sys'</span>, <span class='pystring'>'os'</span>, <span class='pystring'>'re'</span>, <span class='pystring'>'unittest'</span>]</span> <a name="regression.import.3.1"></a><img src="../images/callouts/1.png" alt="1" border="0" width="12" height="12">
<tt class="prompt">&gt;&gt;&gt; </tt><span class="userinput">moduleNames</span>
<span class="computeroutput">['sys', 'os', 're', 'unittest']</span>
<tt class="prompt">&gt;&gt;&gt; </tt><span class="userinput">modules = map(__import__, moduleNames)</span>        <a name="regression.import.3.2"></a><img src="../images/callouts/2.png" alt="2" border="0" width="12" height="12">
<tt class="prompt">&gt;&gt;&gt; </tt><span class="userinput">modules</span>                                       <a name="regression.import.3.3"></a><img src="../images/callouts/3.png" alt="3" border="0" width="12" height="12">
<span class="computeroutput">[&lt;module 'sys' (built-in)&gt;,
&lt;module 'os' from 'c:\Python22\lib\os.pyc'&gt;,
&lt;module 're' from 'c:\Python22\lib\re.pyc'&gt;,
&lt;module 'unittest' from 'c:\Python22\lib\unittest.pyc'&gt;]</span>
<tt class="prompt">&gt;&gt;&gt; </tt><span class="userinput">modules[0].version</span>                            <a name="regression.import.3.4"></a><img src="../images/callouts/4.png" alt="4" border="0" width="12" height="12">
<span class="computeroutput">'2.2.2 (#37, Nov 26 2002, 10:24:37) [MSC 32 bit (Intel)]'</span>
<tt class="prompt">&gt;&gt;&gt; </tt><span class="userinput"><span class='pykeyword'>import</span> sys</span>
<tt class="prompt">&gt;&gt;&gt; </tt><span class="userinput">sys.version</span>
<span class="computeroutput">'2.2.2 (#37, Nov 26 2002, 10:24:37) [MSC 32 bit (Intel)]'</span>
</pre><div class="calloutlist">
               <table border="0" summary="Callout list">
                  <tr>
                     <td width="12" valign="top" align="left"><a href="#regression.import.3.1"><img src="../images/callouts/1.png" alt="1" border="0" width="12" height="12"></a> 
                     </td>
                     <td valign="top" align="left"><tt class="varname">moduleNames</tt> 只是一个字符串列表。没什么特别的，只是这些名字刚好是你可应需而用的可导入模块名。
                     </td>
                  </tr>
                  <tr>
                     <td width="12" valign="top" align="left"><a href="#regression.import.3.2"><img src="../images/callouts/2.png" alt="2" border="0" width="12" height="12"></a> 
                     </td>
                     <td valign="top" align="left">简单得令人惊奇，通过映射 <tt class="function">__import__</tt> 就实现了导入。记住，列表 (<tt class="varname">moduleNames</tt>) 的每个元素将被用来一次次调用函数 (<tt class="function">__import__</tt>) 并以一个返回值构成的列表作为返回结果。
                     </td>
                  </tr>
                  <tr>
                     <td width="12" valign="top" align="left"><a href="#regression.import.3.3"><img src="../images/callouts/3.png" alt="3" border="0" width="12" height="12"></a> 
                     </td>
                     <td valign="top" align="left">所以现在你已经由一个字符串列表构建起了一个实际模块的列表。(你的路径可能不同，这取决于你的操作系统、你安装 Python 的位置、月亮残缺的程度等等。)</td>
                  </tr>
                  <tr>
                     <td width="12" valign="top" align="left"><a href="#regression.import.3.4"><img src="../images/callouts/4.png" alt="4" border="0" width="12" height="12"></a> 
                     </td>
                     <td valign="top" align="left">从这些是真实模块这一点出发，让我们来看一些模块属性。记住，<tt class="varname">modules[0]</tt> <span class="emphasis"><em>是</em></span>  <tt class="filename">sys</tt> 模块，因此，<tt class="varname">modules[0].version</tt> <span class="emphasis"><em>是</em></span> <tt class="varname">sys.version</tt>。所有模块的其他属性和方法也都可用。<tt class="literal">import</tt> 语句没什么神奇的，模块也没什么神奇的。模块就是对象，一切都是对象。
                     </td>
                  </tr>
               </table>
            </div>
         </div>
         <p>现在，你应该能够把这一切放在一起，并搞清楚本章大部分样例代码是做什么的。</p>
      </div>
      <table class="Footer" width="100%" border="0" cellpadding="0" cellspacing="0" summary="">
         <tr>
            <td width="35%" align="left"><br><a class="NavigationArrow" href="data_centric.html">&lt;&lt;&nbsp;数据中心思想编程</a></td>
            <td width="30%" align="center"><br>&nbsp;<span class="divider">|</span>&nbsp;<a href="index.html#regression.divein" title="16.1.&nbsp;概览">1</a> <span class="divider">|</span> <a href="finding_the_path.html" title="16.2.&nbsp;找到路径">2</a> <span class="divider">|</span> <a href="filtering_lists.html" title="16.3.&nbsp;重识列表过滤">3</a> <span class="divider">|</span> <a href="mapping_lists.html" title="16.4.&nbsp;重识列表映射">4</a> <span class="divider">|</span> <a href="data_centric.html" title="16.5.&nbsp;数据中心思想编程">5</a> <span class="divider">|</span> <span class="thispage">6</span> <span class="divider">|</span> <a href="all_together.html" title="16.7.&nbsp;全部放在一起">7</a> <span class="divider">|</span> <a href="summary.html" title="16.8.&nbsp;小结">8</a>&nbsp;<span class="divider">|</span>&nbsp;
            </td>
            <td width="35%" align="right"><br><a class="NavigationArrow" href="all_together.html">全部放在一起&nbsp;&gt;&gt;</a></td>
         </tr>
         <tr>
            <td colspan="3"><br></td>
         </tr>
      </table>
      <div class="Footer">
         <p class="copyright">Copyright © 2000, 2001, 2002, 2003, 2004 <a href="mailto:mark@diveintopython.org">Mark Pilgrim</a></p>
         <p class="copyright">Copyright © 2001, 2002, 2003, 2004, 2005, 2006, 2007 <a href="mailto:python-cn@googlegroups.com">CPyUG (邮件列表)</a></p>
      </div>
   </body>
</html>